/*
 * Copyright (c) 2010. Dylan Schell.
 */

package nl.xs4all.dylanschell.deckprinter.pages;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.util.string.JavascriptUtils;

import java.util.Arrays;

/**
 * Created by IntelliJ IDEA.
 * User: dylan
 * Date: Dec 8, 2010
 * Time: 7:35:40 AM
 * To change this template use File | Settings | File Templates.
 */
public class Chart extends Panel implements IHeaderContributor
{
    private String chartType;
    private String width = "400px";
    private String height = "400px";
    private String title = "Manacurve";
    private String seriesTitle = "Cards with Converted Mana Cost";
    private WebMarkupContainer chart;
    private int[] series;

    public Chart(String id)
    {
        super(id);
        chart = new WebMarkupContainer("chart");
        chart.setOutputMarkupId(true);
        chart.add(new AttributeModifier("style", true, new PropertyModel<String>(this, "chartContainerStyle")));
        add(chart);
    }

    @Override
    public void renderHead(IHeaderResponse response)
    {
        String script = "var chart = new Highcharts.Chart(" +
                "{" +
                "chart: {" +
                "    renderTo: '" + chart.getMarkupId() + "'," +
                "    defaultSeriesType: 'column'" +
                "}," +
                "title: {" +
                "    text: '" + title + "'" +
                "}," +
                "xAxis: {" +
                "    title: 'test'," +
                "    categories: " + Arrays.toString(series) +
                "}," +
                "yAxis: {" +
                "    title: 'Cards'" +
                "}," +
                "series: [{" +
                "    name: '" + seriesTitle + "'," +
                "    data: [0,1,2,3,4,5,6,7,8,9]" +
                "}]" +
                "}" +
                ");";
        response.renderOnDomReadyJavascript(script);
    }

    public String getChartContainerStyle()
    {
        return "width: " + width + "; height: " + height + "; margin: 0; auto";
    }

    public String getChartType()
    {
        return chartType;
    }

    public void setChartType(String chartType)
    {
        this.chartType = chartType;
    }

    public String getWidth()
    {
        return width;
    }

    public void setWidth(String width)
    {
        this.width = width;
    }

    public String getHeight()
    {
        return height;
    }

    public void setHeight(String height)
    {
        this.height = height;
    }

    public void setSeries(int[] series)
    {
        this.series = series;
    }
}